new Vue({
    el: '#app',
    data() {
        return {
            fileList: [],
            tableData: [],
            form: {
                page: 1,
                limit: 15,
            },
            loading: false,
            success: 0,
            error: 0,
            supplier:[],//供应商
            
        }
    },
    mixins:[importMixin],
    methods: {
        submitUpload(i = 0) {
            const that = this;
            if (i < this.tableData.length) {
                this.loading = true;
                if (this.tableData[i].isError) {
                    this.tableData[i].isStatus = '2';
                    this.error++;
                    this.submitUpload(i + 1);
                }  else if (!this.tableData[i].User_Number) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '联系人手机号不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!this.tableData[i].ID_Name) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '联系人姓名不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else { //校验通过
                    //1.通过手机号查询该用户是否存在
                    let obj = {
                        User_Number: that.tableData[i].User_Number,
                        Pass_Word: that.tableData[i].Pass_Word,
                        Image:'',
                        ID_Name: that.tableData[i].ID_Name,
                        Name: that.tableData[i].Name,
                        Sex: that.tableData[i].Sex,
                        Mail:'',
                        Creation_Datetime:getYMDHMSDate(),
                        Children:{
                            base_system_user_role: [{
                                System_User_Role_SerialNumber: '',
                                User_Number: that.tableData[i].User_Number,
                                Role_SerialNumber: 'supplier',
                                Other_SerialNumber: that.tableData[i].Supplier_SerialNumber,
                            }],
                        }
                    }
                    that.submitForm({ Data: JSON.stringify({ Add: { base_system_user_info: [obj] } }) }, (res) => {
                        if (res.Status == 'success') {
                            that.tableData[i].isStatus = '1';
                            that.tableData[i].Cause = '',
                            that.success++;
                        } else {
                            that.tableData[i].isStatus = '2';
                            that.tableData[i].Cause = '该手机号已注册，无法继续注册'
                            that.error++;                  
                        }
                        that.submitUpload(i + 1);
                    })
                }
            } else if (i != 0) {
                this.loading = false;
                this.$message.success("批量导入结束")
            } else {
                this.$message({
                    message: '请先导入模板后再上传',
                    type: 'warning'
                })
            }
        },
        submitForm(data, callback) {
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalSave`,
                type: 'POST',
                data,
                success: function (res) {
                    callback(res)
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest); 
                },
                dataType: "json"
            })
        },
        handleChange({ raw }) {//导入监听文件
            const that = this;
            var reader = new FileReader();
            reader.readAsBinaryString(raw);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                //1.查询商品信息，并展示到页面上
                let Condition = "";
                let Supplier = [];
                json.forEach((item,index)=>{
                    let Supplier_SerialNumber = item['供应商内码']?item['供应商内码']:'';
                    if (!Supplier.some(i=>i==Supplier_SerialNumber)) {
                        Supplier.push(Supplier_SerialNumber);
                        if (index==0) {
                            Condition = Condition + "Supplier_SerialNumber='" + Supplier_SerialNumber + "'\r\n"
                        } else {
                            Condition = Condition + " or Supplier_SerialNumber='" +  Supplier_SerialNumber + "'\r\n"
                        }
                    }
                })
                $.ajax({
                    headers:requestHeaders(),
                    type: 'POST',
                    url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                    data:{
                        Data: 'business_supplier_info',
                        Condition:`Row_Status='0' and (${Condition})`,
                    },
                    success: function (Result) {
                        console.log(Result);
                        let arr = [];
                        json.forEach(item => {
                            let Supplier_SerialNumber = item['供应商内码']?item['供应商内码']:'';
                            let Name = item['联系人昵称']?item['联系人昵称']:'';
                            let User_Number = item['联系人手机号']?item['联系人手机号']:'';
                            let ID_Name = item['联系人姓名']?item['联系人姓名']:'';
                            let Pass_Word = item['登录密码']?item['登录密码']:'Aa123456';
                            let Sex = item['性别']=='女'?'1':'0';
                            if (Result.some(i=>i.Supplier_SerialNumber == Supplier_SerialNumber)) {
                                arr.push({
                                    ...Result.filter(i=>i.Supplier_SerialNumber == Supplier_SerialNumber)[0],
                                    User_Number,
                                    Name,
                                    ID_Name,
                                    Pass_Word,
                                    Sex,
                                    isEdit: false,
                                    isStatus: '0',
                                    Cause:'',
                                })
                            } else{
                                arr.push({
                                    User_Number,
                                    Name,
                                    ID_Name,
                                    Pass_Word,
                                    Sex,
                                    isError:true,
                                    isStatus: '2',
                                    Cause:'未获取到供应商信息',
                                })
                            }         
                        })
                        that.tableData = arr;
                    },
                    complete: function (XMLHttpRequest) {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json"
                });
            }
        },
        handleSizeChange(val) {
            this.form.page = 1;
            this.form.limit = val;
        },
        handleCurrentChange(val) {
            this.form.page = val;
        },
        handleEdit(row) {//修改当前行
            row.isEdit = !row.isEdit
        },
        handleRemove(index) {//移除当前行
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index);
            }).catch(() => { });
        },
        handleClean() {//清空
            this.$confirm('是否要清空导入数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = []
            }).catch(() => { });
        },
    },
})